package org.hawk.gwt.ppc.utils;

import junit.framework.TestCase;

public class NameTestCase extends TestCase {
	
	public void testJavaToFs(){
		assertEquals("Mock.java",Name.javaToFs("Mock"));
		assertEquals("org/hawk/Mock.java",Name.javaToFs("org.hawk.Mock"));
		assertEquals("file.xml",Name.javaToFs("file","xml"));
		assertEquals("file.xml",Name.javaToFs("file",".xml"));
		assertEquals("org/hawk/file.xml",Name.javaToFs("org.hawk.file","xml"));
		assertEquals("org/hawk/file.xml",Name.javaToFs("org.hawk.file",".xml"));
	}
	
	public void testFsToJava(){
		assertEquals("MyName", Name.fsToJava("MyName.java"));
		assertEquals("org.hawk.Mock", Name.fsToJava("org.hawk.Mock.java"));
		try{
			Name.fsToJava("org.hawk.Mock.class");
			fail();
		}
		catch(IllegalArgumentException ex){}
	}
	
	public void testIsJavSrc(){
		assertTrue(Name.isJavaSrc("MyClass.java"));
		assertTrue(Name.isJavaSrc("Mock.java"));
		assertFalse(Name.isJavaSrc("package-info.java"));
		assertFalse(Name.isJavaSrc("Mock.xml"));
		assertFalse(Name.isJavaSrc("Mock.class"));
	}

	public void testIsValidPackageName(){
		assertTrue(Name.isValidPackageName(""));
		assertTrue(Name.isValidPackageName("org"));
		assertTrue(Name.isValidPackageName("org.hawk"));
		assertTrue(Name.isValidPackageName("org.hawk.$gwt"));
		assertTrue(Name.isValidPackageName("org.hawk._gwt"));
		assertTrue(Name.isValidPackageName("org.hawk.gwt_info"));
		assertTrue(Name.isValidPackageName("org.hawk.gwt$info"));
		assertTrue(Name.isValidPackageName("org.hawk.gwt1"));
		assertFalse(Name.isValidPackageName(null));
		assertFalse(Name.isValidPackageName("."));
		assertFalse(Name.isValidPackageName("org."));
		assertFalse(Name.isValidPackageName("org..hawk"));
		assertFalse(Name.isValidPackageName("org.hawk.1gwt"));
	}
	
	public void testChildJavaName(){
		assertEquals("Mock", Name.getChildJavaName("", "Mock"));
		assertEquals("org.Mock", Name.getChildJavaName("org", "Mock"));
		assertEquals("org.hawk.gwt.Mock", Name.getChildJavaName("org.hawk.gwt", "Mock"));
	}
	
	public void testParentJavaName(){
		assertEquals("", Name.getParentJavaName("Mock"));
		assertEquals("org", Name.getParentJavaName("org.Mock"));
		assertEquals("org.hawk.gwt", Name.getParentJavaName("org.hawk.gwt.Mock"));
	}
	
	public void testDirectSubPackage() {
		assertTrue(Name.isDirectSubPackage("org", ""));
		assertTrue(Name.isDirectSubPackage("org.hawk", "org"));
		assertFalse(Name.isDirectSubPackage("org.hawk", ""));
		assertFalse(Name.isDirectSubPackage("org", "org.hawk"));
		assertFalse(Name.isDirectSubPackage("org.hawk", "org.hawk"));
		assertTrue(Name.isDirectSubPackage("org.hawk.a", "org.hawk"));
		assertFalse(Name.isDirectSubPackage("", ""));
		assertFalse(Name.isDirectSubPackage("org.hawk.gwt", "org"));
		assertFalse(Name.isDirectSubPackage("org.hawk.gwt.internal", "org"));
	}
	
	public void testGetCommonPackage(){
		assertEquals("", Name.getCommonPackage("org.hawk.test", ""));
		assertEquals("", Name.getCommonPackage("", "org.hawk.test"));
		assertEquals("", Name.getCommonPackage("org.hawk.test", "com.google"));
		assertEquals("org", Name.getCommonPackage("org.google", "org.hawk.test"));
		assertEquals("org", Name.getCommonPackage("org.hawk.test", "org.google"));
		assertEquals("org.hawk", Name.getCommonPackage("org.hawk.test", "org.hawk.test2.common"));
		assertEquals("org.hawk", Name.getCommonPackage("org.hawk.test2.common", "org.hawk.test"));
		assertEquals("org.hawk.test", Name.getCommonPackage("org.hawk.test", "org.hawk.test"));
		assertEquals("org.hawk", Name.getCommonPackage("org.hawk.test", "org.hawk.testcase"));
		assertEquals("", Name.getCommonPackage("", ""));
	}
}
